package com.longma.server.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.longma.server.entity.OperationLog;
import com.longma.server.pojo.UserResponseEntity;
import com.longma.server.pojo.dto.OperationLogDTO;
import com.longma.server.pojo.dto.OperationLogUpdateDTO;
import com.longma.server.pojo.dto.PageRequestDTO;
import com.longma.server.service.IOperationLogService;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;

@RestController
@RequestMapping("/api/operation-log")
public class OperationLogController {
    @Autowired
    IOperationLogService operationLogService;

    @ApiOperation(value = "新增操作日志")
    @PostMapping("/add")
    public UserResponseEntity<OperationLog> addOperationLog(@Valid @RequestBody OperationLogDTO addRequestDTO){
        return UserResponseEntity.success(operationLogService.addOperationLog(addRequestDTO));
    }

    @ApiOperation(value = "根据操作日志id删除操作日志")
    @PostMapping("/del")
    public UserResponseEntity<Boolean> deleteOperationLogById(@NotNull(message = "操作日志id不能为空")  @RequestParam Integer id){
        return UserResponseEntity.success(operationLogService.deleteOperationLogById(id));
    }

    @ApiOperation(value = "根据操作日志id列表删除操作日志")
    @PostMapping("/dellist")
    public UserResponseEntity<Boolean> deleteOperationLogByIds(@RequestParam(name="ids") @Size(min = 1,message = "操作日志id列表不能为空")
                                                                      List<@Valid @NotNull(message = "操作日志id不能为空") Integer> ids){
        return UserResponseEntity.success(operationLogService.deleteOperationLogByIds(ids));
    }

    @ApiOperation(value = "更新操作日志")
    @PostMapping("/update")
    public UserResponseEntity<OperationLog> updateOperationLog(@Valid @RequestBody OperationLogUpdateDTO updateRequestDTO){
        return UserResponseEntity.success(operationLogService.updateOperationLog(updateRequestDTO));
    }

    @ApiOperation(value = "获取所有操作日志(分页)")
    @PostMapping("/list")
    public UserResponseEntity<IPage<OperationLog>> getOperationLogByPage(@ApiParam(value = "分页参数",required = true) @Valid @RequestBody PageRequestDTO pageRequestDTO){
        return UserResponseEntity.success(operationLogService.getOperationLogByPage(pageRequestDTO.getPageNum(),
                pageRequestDTO.getPageSize(),pageRequestDTO.getAll(),pageRequestDTO.getConditions()));
    }
}
